using System;
using System.IO;
using System.Text;
using Serilog;
using Serilog.Core;
using Serilog.Sinks.SystemConsole.Themes;

namespace AvaloniaApplication1.Util;

public class AppLogger
{
     private static readonly Logger _logger;

    static AppLogger()
    {
        var logName = "TestWebApp"; // 日志文件名(项目名)
        // 使用应用程序的基目录，确保在任意工作目录下都能创建
        var baseDir = AppDomain.CurrentDomain.BaseDirectory;
        // 项目根目录
        var projectRoot = Path.GetFullPath(Path.Combine(baseDir, "..", "..", ".."));
        var logDirectory = Path.Combine(projectRoot, "log");
        var logFilePath = Path.Combine(logDirectory, $"{logName}_.log");

        // 创建日志目录
        if (!Directory.Exists(logDirectory))
        {
            Directory.CreateDirectory(logDirectory);
            Console.WriteLine($"创建目录: {logDirectory}");
        }

        // 日志输出格式
        const string outputTemplate =
            "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} | [{Level:u3}] | {SourceContext} | {Message:lj}{NewLine}{Exception}";
        
        _logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Async(c => c.Console(
                outputTemplate: outputTemplate,
                theme: AnsiConsoleTheme.Code
                ))
            .WriteTo.Async(c =>
                c.File(logFilePath, outputTemplate: outputTemplate, rollingInterval: RollingInterval.Day,encoding:Encoding.UTF8))
            .CreateLogger();

        _logger.Information("=====App Logger初始化完成, 执行项目{LogName}=====", logName);
    }

    /// <summary>
    /// 信息日志
    /// </summary>
    /// <param name="message">显示消息</param>
    /// <param name="propertyValues"></param>
    public static void Information(string message, params object[] propertyValues)
    {
       _logger.Information(message, propertyValues);
    }

    /// <summary>
    /// 警告日志
    /// </summary>
    /// <param name="message">显示消息</param>
    /// <param name="propertyValues"></param>
    public static void Warning(string message, params object[] propertyValues)
    {
        _logger.Warning(message, propertyValues);
    }

    /// <summary>
    /// 错误日志
    /// </summary>
    /// <param name="exception">异常</param>
    /// <param name="message">显示消息</param>
    /// <param name="propertyValues"></param>
    public static void Error(Exception exception, string message, params object[] propertyValues)
    {
       _logger.Error(exception, message, propertyValues);
    }
    public static void PublicError(string message, params object[] propertyValues)
    {
        _logger.Error( message, propertyValues);
    }
    /// <summary>
    /// 调试日志
    /// </summary>
    /// <param name="message">显示消息</param>
    /// <param name="propertyValues"></param>
    public static void Debug(string message, params object[] propertyValues)
    {
       _logger.Debug(message, propertyValues);
    }

    /// <summary>
    /// 严重错误日志
    /// </summary>
    /// <param name="exception">异常</param>
    /// <param name="message">显示消息</param>
    /// <param name="propertyValues"></param>
    public static void Fatal(Exception exception, string message, params object[] propertyValues)
    {
       _logger.Fatal(exception, message, propertyValues);
    }
}